﻿ /*************************************************************************
File:   as/publish.as
Author: Duykham
UpDate: 11/09/2006
TODO:   Using for publishCam.FLA
/*************************************************************************/
trace ("#included: publish.as");
///////////////////////////////////////////////////////////////////////////
//TODO: included files
///////////////////////////////////////////////////////////////////////////
//TODO: khai bao bien
// Create the Microphone and Camera objects.
//var cameras_cb:mx.controls.ComboBox;
var user_mic = Microphone.get ();
var user_cam = Camera.get (_root.camNumber);
// Create the NetConnection object.
var publish_nc = new NetConnection ();
// Create listener objects to be used with the components.
var connectListener = new Object ();
var rsoMessages;
// Variable to store the RSO.
var rsoCameras;
// Variable to store the RSO.
var stream_ns;
// NetStream for streaming.
var record_ns;
// NetStream for recording.
var send_ns;
// NetStream for sending data.
var sMessageTitle;
// Store title of message being recorded.
var nameOfCam = _root.camName;
var displayName = _root.disName;
var myMotionLevel = _root.userMotionLevel;
trace ("myMotionLevel: " + myMotionLevel);
//Khoi tao bien thuoc component ProgressBar
var motion_pb : mx.controls.ProgressBar;
motion_pb.mode = "manual";
motion_pb.label = "Motion level %3%%";
var Active;
//trang thai hoat dong cua Cam, bien duoc truyen qua sendData
var len;
var oldDate;
//co hieu de clearInterval ham khoi dong ShareObject
var flag = false;
///////////////////////////////////////////////////////////////////////////
//TODO: Initialize
preview_btn.enabled = false;
camID_txt._visible = false;
cam_txt._visible = false;
motion_txt._visible = false;
priority_txt._visible = false;
display_txt._visible = false;
status_mc.onRollOver = function ()
{
	toolTip.show (title_txt, 90, 24, 125, 127);
};
status_mc.onRollOut = function ()
{
	toolTip.hide ();
};
// Initialize the microphone threshold and camera mode and quality.
user_mic.setSilenceLevel (10, 2000);
user_cam.setMode (320, 240, 5);
//Set Mode chi nen de la 5 fps
user_cam.setMotionLevel (50, 2000);
user_cam.setQuality (0, 100);
user_cam.setLoopback (true);
// Set the loopback to true so user can see what the output stream looks like.
// Connect to the FlashMediaServer application.
//publish_nc.connect ("rtmp://"+_root.CFG.IPaddress+"/ProconDemoApp");
publish_nc.connect (_root.CFG.fmsApp);
send_ns = new NetStream (publish_nc);
// Define the listener for the Publish button.
connectListener.click = function (event)
{
	// The Record toggle button has a selected value of true or false.
	// Neu dung, bat dau phat hinh chuyen dong
	if (event.target.selected)
	{
		user_cam.setMotionLevel (myMotionLevel, 2000);
		// Set the label of the button to Stop while in Record mode.
		event.target.label = "Pause";
		title_txt = "Connected...";
		status_mc.gotoAndStop (1);
		toolTip.show (title_txt, 90, 24, 125, 127);
		exit_mc._visible = false;
		preview_btn.enabled = true;
		// Display the video in the Video object.
		//on 20060921
		record_vid.attachVideo (user_cam);
		// Create the NetStream object, and add the audio and video.
		stream_ns = new NetStream (publish_nc);
		stream_ns.attachAudio (user_mic);
		stream_ns.attachVideo (user_cam);
		sMessageTitle = displayName;
		// Publish the stream.
		stream_ns.publish (sMessageTitle);
		//////////UpLoad thong so cac Cameras////////////////
		rsoCameras = SharedObject.getRemote ("cams/" + clock.strDate, publish_nc.uri, true);
		rsoCameras.connect (publish_nc);
		/////////// Ghi lai ngay gio phat hinh vao log//////////////
		rsoMessages = SharedObject.getRemote ("log/" + clock.strDate, publish_nc.uri, true);
		rsoMessages.connect (publish_nc);
		/*
		//////////UpLoad thong so cac Cameras////////////////
		
		rsoCameras = SharedObject.getRemote ("cams/" + clock.strDate, publish_nc.uri, true);
		rsoCameras.connect (publish_nc);
		
		*/
		// Chen them mot cam vao file .fso
		addNewCam (displayName);
	} 
	else
	{
		event.target.label = "Continue";
		title_txt = "Disconnected";
		status_mc.gotoAndStop (3);
		toolTip.show (title_txt, 90, 24, 125, 127);
		// Clear the video and audio from the Video and NetStream objects.
		record_vid.attachVideo (null);
		//record_vid.clear ();
		stream_ns.attachAudio (null);
		stream_ns.attachVideo (null);
		// Stop publishing and close the stream.
		stream_ns.publish (false);
		stream_ns.close ();
		rsoMessages.connect (false);
		//removeCam (cam_txt.text);
		removeCam (displayName);
		//rsoCameras.connect (false);
		
	}
};
user_cam.onActivity = function (isActive : Boolean)
{
	var themeColor : String = (isActive) ? "haloGreen" : "haloOrange";
	motion_pb.setStyle ("themeColor", themeColor);
	if (isActive && title_txt == "Connected...")
	{
		if (clock.strDate != undefined && user_cam.activityLevel < 100)
		{
			Active = 1;
			callVB (nameOfCam, user_cam.activityLevel);
			//Edit on 20060930 - them vao user_activityLevel
			title_txt = "Recording...";
			status_mc.gotoAndStop (2);
			toolTip.show (title_txt, 90, 24, 125, 127);
			_root.preview_vid.attachVideo (null);
			_root.preview_vid.attachVideo (user_cam);
			record_ns = new NetStream (publish_nc);
			record_ns.attachAudio (user_mic);
			record_ns.attachVideo (user_cam);
			record_ns.publish ("archive/" + clock.strDate + "/" + displayName, "append");
			if (rsoMessages.data.logs == undefined)
			{
				rsoMessages.data.logs = new Array ();
			}
			if (clock.strTime != undefined && user_cam.activityLevel < 100)
			{
				rsoMessages.data.logs.push ( 
				{
					label : clock.strTime + " [" + displayName + "]", data : "(" + user_cam.activityLevel + "%)"
				});
			}
		}
	} 
	else
	{
		Active = 0;
		record_ns.attachVideo (null);
		record_ns.attachAudio (null);
		// Stop publishing and close the stream.
		record_ns.publish (false);
		record_ns.close ();
		if (title_txt != "Disconnected")
		{
			title_txt = "Connected...";
			toolTip.show (title_txt, 90, 24, 125, 127);
			status_mc.gotoAndStop (1);
		}
	}
};
pblsh_btn.toggle = true;
pblsh_btn.addEventListener ("click", connectListener);
pblsh_btn.label = "Start Streaming";
title_txt = "Stopped";
cbtPlayback.addEventListener ("click", playbackListener);
cbtPlayback.label = "Play Message";
//Khoi dong lai ket noi
restart = function ()
{
	if (pblsh_btn.label == "Pause")
	{
		stream_ns.attachAudio (null);
		stream_ns.attachVideo (null);
		// Stop publishing and close the stream.
		stream_ns.publish (false);
		stream_ns.close ();
		rsoMessages.connect (false);
		removeCam (displayName);
		rsoCameras.connect (false);
		rsoMessages = SharedObject.getRemote ("log/" + clock.strDate, publish_nc.uri, true);
		rsoMessages.connect (publish_nc);
		rsoCameras = SharedObject.getRemote ("cams/" + clock.strDate, publish_nc.uri, true);
		rsoCameras.connect (publish_nc);
		addNewCam (displayName);
		stream_ns = new NetStream (publish_nc);
		stream_ns.attachAudio (user_mic);
		stream_ns.attachVideo (user_cam);
		stream_ns.publish (clock.strDate);
	}
	oldDate = clock.strDate;
};
listCam.change = function (event)
{
	user_cam = Camera.get (cameras_cb.selectedIndex);
	trace ("dfgdfgdfh");
}
cameras_cb.addEventListener ("change", listCam);
//TODO: add a new cam
addNewCam = function (camStr : String)
{
	trace ("addNewCam = function (camStr : String)");
	var camName = camStr;
	trace ("camName: " + camName);
	var flag : Boolean = false;
	trace (flag);
	if (rsoCameras.data.cams == undefined)
	{
		trace ("if (rsoCameras.data.cams == undefined)");
		rsoCameras.data.cams = new Array ();
		rsoCameras.data.cams.push ( 
		{
			label : camName, data : camName
		});
	} 
	else
	{
		var camArr = rsoCameras.data.cams;
		for (var i : Number = 0; i < camArr.length; i ++)
		{
			//kiem tra xem ten Camera da ton tai hay chua?
			if (camName == camArr [i].label)
			{
				//bat co hieu
				flag = true;
			}
		}
	
	if (flag == false)
	{
		rsoCameras.data.cams.push ( 
		{
			label : camName, data : camName
		});
		trace ("chen them mot camera: " + camName);
	}
	}
};
//TODO: remove cam
removeCam = function (camStr : String)
{
	var camName = camStr;
	var camArr = rsoCameras.data.cams;
	for (var i : Number = 0; i < camArr.length; i ++)
	{
		if (camName == camArr [i].label)
		{
			camArr.splice (i, 1);
			// loai bo Cam danh sach
		}
	}
};
//
this.onEnterFrame = function ()
{
	/*
	if (clock.strDate != undefined && flag == false)
	{
	clearInterval (intervalID);
	//addNewCam ("NoCameras");
	pblsh_btn.onRelease ();
	flag = true;
	}
	*/
	if (this.title_txt != "Disconnected")
	{
		motion_pb.setProgress (user_cam.activityLevel, 100);
		//send_ns = new NetStream (publish_nc);
		//tao mot NetStream moi dung de truyen bien den client
		send_ns.publish ("sendData_" + displayName);
		//trace("displayName: "+displayName);
		//bien duoc truyen qua goi sendData
		send_ns.send ("activeLevel", user_cam.activityLevel);
		//truyen activityLevel cua cam
		send_ns.send ("onActive", Active);
		//truyen trang thai cua cam
		if (oldDate != clock.strDate)
		//khi chuyen ngay se goi ham restart
		//de khoi dong lai ket noi
		
		{
			restart ();
		}
	}
};
//on exit
exit_mc.onRelease = function ()
//this.close = function ()
{
	stream_ns.close ();
	send_ns.close ();
	publish_nc.close ();
	gotoAndPlay ("step1");
};
callVB = function (camName : String, motionlevel : String)
{
	trace ("camName: " + camName);
	trace ("motionlevel: " + motionlevel )
	FSCommand (camName, motionlevel);
	//Edit on 20060930 - them vao motion level
	//truyen CamName cho VB
};
//
initialSO = function ()
{
	intervalID = setInterval (function ()
	{
		//////////UpLoad thong so cac Cameras////////////////
		rsoCameras = SharedObject.getRemote ("cams/" + clock.strDate, publish_nc.uri, true);
		rsoCameras.connect (publish_nc);
		/////////// Ghi lai ngay gio phat hinh vao log//////////////
		rsoMessages = SharedObject.getRemote ("log/" + clock.strDate, publish_nc.uri, true);
		rsoMessages.connect (publish_nc);
		//////////////////////////////////
		
	}, 100);
};
//initialSO ();
oldDate = clock.strDate;
//if (_root.clock.strDate != undefined){
//pblsh_btn.onRelease();
//}
pblsh_btn.onRelease ();
//pblsh_btn.onRelease();
